package com.fairytask.ui.main
{
	import com.adobe.utils.StringUtil;
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.Scroll.ScrollBar;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.checkBox.CheckBox;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.debug.DebugWindow;
	import com.fairytask.event.ChatEvent;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.chat.ChatList;
	import com.fairytask.ui.item.chat.ChatListCheckBoxItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	
	/**
	 * 聊天UI 
	 * @author xujh
	 * 
	 */
	public class ChatUI extends FrameUI
	{
		//显示组件=====================================================================
		private static var _self	:ChatUI;
		public var inputMc         :MovieClip;//输入框背景
		public var chat_mc 		:MovieClip;
		public var chanelList		:ChatList;
		public var btn_current 	:Button;
		public var btn_clear		:Button;//清除按钮
		public var btn_stop 		:CheckBox;
		public var btn_enter 		:Button;//输入按钮
		public var chatInput 		:Input;
		public var but_move 		:Button;
		
		private var _chatScrollPanel	:ScrollPanel;
		private var horizontalBar		:ScrollBar;	//添加滚动条
		
		//聊天框数据
		private var input_size		:int = 13;
		private var chatIsEmpty	:Boolean	=	true; //空的
		private const FONT_FAMILY	:String = '微软雅黑';
		private const TEXT_LAYOUT_NAMESPACE:String ="http://ns.adobe.com/textLayout/2008";
		
		private var _configXML		:XML;
		private const XML_PATH:String ="res/icon.xml";
		
		//数据变量=========================================================================
		//false:不禁止滚屏，true:禁止滚屏
		private var stopAutoShowNum:Boolean;
		//记录玩家发送的信息:最新10条
		private var sendMsg			:Array=null;
		private var curSendMsgIndex	:int;
		private var tpNotSendMsg		:String;
		private var initFlag			:int;
		//聊天区域显示信息
		private var contentMsg			:Array=null;
		private var channels			:int=0;
		private var target				:String=" ";
		private var nowChannels		:int=0;
		private var listBtnInfo		:Array;
		private var recieverName		:String=null;
		private var sendContent		:String=null;	
		//存储所有的频道内容
		private var _chatData			:Array	=	[];	
		private var _curShow			:Array	= 	null;
		private var _isEquip			:Boolean;		//是否装备展示
		private var _equipName			:String;		//输入框内的装备名字
		private var _equipObjId		:Point;			//装备ObjID
		private var _isShowTips		:Boolean;
		private var _equipColor		:int	=	-1;	//物品颜色
		//锁屏数据记录
		private var _cacheArr		:Array	=	[];
		private var _stopScroll		:Number	=	1;	
		//聊天最大行数
		private const MAX_LINE	:int	=	50;
		
		//常量=============================================================================
		public static const	CHANNEL_CURRENT		:int	=	0;//当前
		public static const	CHANNEL_PRIVATE		:int	=	1;//私聊
		public static const	CHANNEL_TEAM		:int	=	2;//队伍
		public static const	CHANNEL_COUNTY		:int	=	3;//门派
		public static const	CHANNEL_PARTY		:int	=	4;//帮会
		public static const	CHANNEL_WORLD		:int	=	5;//世界
		public static const	CHANNEL_TRADE		:int	=	6;//交易
		public static const	CHANNEL_BROAD		:int	=	7;//喇叭
		public static const	CHANNEL_SYSTEM		:int	=	8;//系统
		public static const	CHANNEL_TIP			:int	=	9;//本地提醒
		public static const 	FLAG_PRIVATE_FAIL	:int	=	10;//私聊失败(玩家不存在)
		
		private var otherEquipId		:Point	=	new Point();
		private var chatText:Text;
		
		public function ChatUI(popupUI:Boolean=false)
		{	
			super(popupUI);
			init();
			initEvent();	
		}
		
		private function init():void
		{
			initX = this.x;
			initY = this.y;
			//鼠标事件管理
			chat_mc.mouseEnabled	=	false;
			windowBG.mouseEnabled	=	false;
			
			initChatTxt();
			windowBG.addChild(btn_clear);
			windowBG.addChild(btn_stop);
			//初始化XML
			initXML();
			//=============================================
			sendMsg	= new Array();
			contentMsg	=	new Array();
			//初始化内容框默认要显示的频道
			_curShow	=	[CHANNEL_CURRENT,CHANNEL_PRIVATE,CHANNEL_PARTY,CHANNEL_SYSTEM,CHANNEL_TIP];
			//初始化聊天列表信息
			initListItemInfos();
			chanelList = new ChatList(63,160,31,listBtnInfo,"button_019","checkbox_001");
			chanelList.visible	= false;
			addChild(chanelList);
			chanelList.y=90;
			chatInput.maxChars=30;
			
			initChatContent();
			initSpeakerContent();
		}
		
		private function initChatTxt():void
		{
			chatText	=	new Text(350,93,"vScrol_1","vScrol_5");
			
			var scrollSkin:ScrollSkin=new ScrollSkin("vScrol_1","vScrol_5");
			_chatScrollPanel = new ScrollPanel(new Rectangle(0,0,350,93),chatText,scrollSkin);
			_chatScrollPanel.x = 1;
			_chatScrollPanel.y = 70;
			windowBG.addChild(_chatScrollPanel);
		}
		
		/**
		 * 初始化iconXML 
		 * 
		 */
		private function initXML():void
		{
			var tmpLoader	:URLLoader	=	new URLLoader();
			tmpLoader.addEventListener(Event.COMPLETE,onLoaded);
			tmpLoader.load(new URLRequest(XML_PATH));
		}
		
		/**
		 * 加载完毕 
		 * @param evt
		 * 
		 */
		private function onLoaded(evt:Event):void
		{
			_configXML	=	new XML(evt.target.data);
		}
		
		public function setEquipShow(equipName:String,equipId:int,equipObjId:Point,equipColor:int):void
		{
			_isEquip	=	true;
			_equipName	=	equipName;
			chatInput.htmlStr	=	_equipName;
			_equipObjId	=	equipObjId;
			_equipColor	=	equipColor;
		}
		
		/**
		 * 单例获取ChatUI对象 
		 * @return 
		 * 
		 */		
		public static function getInstance():ChatUI
		{
			if(_self == null)
			{
				_self	=	new ChatUI();
			}
			return _self;
		}
		
		/**
		 * 初始化:data格式:{label:"当前"} 的数组
		 * 
		 */		
		private function initListItemInfos():void
		{
			listBtnInfo		=	new Array();
			var obj:Object	=	new Object();
			obj["label"]	=	StaticIni.getSwfLangStr("1000000253");//当前
			listBtnInfo.push(obj);
			
			obj				=	new Object();
			obj["label"]	=	StaticIni.getSwfLangStr("1000000255");//私聊
			listBtnInfo.push(obj);
			
			obj				=	new Object();
			obj["label"]	=	StaticIni.getSwfLangStr("1000000254");//帮派
			listBtnInfo.push(obj);
		}
		
		/**
		 * 初始化添加所有事件侦听 
		 * 
		 */		
		private function initEvent():void
		{
			//服务器事件========================================
			model.addEventListener(ChatEvent.BROADCAST_MESSAGE,onBroadCast);//系统公告信息
			//=================================================
			
			//聊天窗口和中间显示效果都有的提示
			model.addEventListener(ChatEvent.CLIENT_ALL_SHOW_MSGINFO,onAllInfoShow);
			//只在聊天框显示的消息
			model.addEventListener(ChatEvent.CLIENT_CHAT_SHOW_MSGINFO,onChatShow);
			//好友系统请求的私聊
			model.addEventListener(ChatEvent.FRIEND_UI_PRIVATE_CHAT_MSG,friendSysAskPrivateChat);
			//=================================================
			
			//UI事件============================================
			btn_enter.addEventListener(MouseEvent.CLICK,onEnter);
			btn_clear.addEventListener(MouseEvent.CLICK,onClear);
			chatInput.addEventListener(MouseEvent.MOUSE_UP,onInputMouseUP);
			chatInput.addEventListener(KeyboardEvent.KEY_DOWN,onKeyEvent);
			
			btn_current.addEventListener(MouseEvent.CLICK,onListShow);
			chanelList.addEventListener(ChatEvent.CHAT_LIST_BTN_CLICK,onList);
			//禁止自动滚屏
			btn_stop.addEventListener(MouseEvent.CLICK,stopAutoShow);
			//【当前】和【喇叭】频道不可以屏蔽
			chanelList.checkBoxData[0].addEventListener(ChatEvent.CHAT_LIST_CB_NOT_PIN,tipShowNotPin);
			this.addEventListener(MouseEvent.ROLL_OUT,onChatOver);
			GameStage.stage.addEventListener(MouseEvent.CLICK,onHideTips);
			for(var i:int=0;i<chanelList.checkBoxData.length;i++)
			{
				if(i!=0&&i!=7)
				{
					chanelList.checkBoxData[i].addEventListener(ChatEvent.CHAT_LIST_CB_PIN,tipShowPin);
					chanelList.checkBoxData[i].addEventListener(ChatEvent.CHAT_LIST_CB_BACK_NOT_PIN,tipShowBackNotPin);
				}else
				{
					continue;
				}
			}
			//=================================================
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
		}
		
		protected function onAdded(evt:Event):void
		{
			//全局事件监听
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDownEnter);
		}
		
		/**
		 * 焦点改变继续聚焦 
		 * @param evt
		 * 
		 */
		private function onFocusChange(evt:FocusEvent):void
		{
			if(!_self.isShow)
			{
				return;
			}
			stage.focus	=	chatInput.inputTxt;
		}
		
		
		
		private function onHideTips(evt:MouseEvent):void
		{
			if(_isShowTips)
			{
				TipsManage.hideTips();			
				_isShowTips	=	false;	
			}
		}
		
		/**
		 * 公告消息 
		 * @param evt
		 * 
		 */		
		private function onBroadCast(evt:ChatEvent):void
		{
			onChatShow(evt);
		}
		
		/**
		 * 附加聊天显示 
		 * @param pChannel 频道
		 * @param pTxt 文字
		 * 
		 */
		private function appendTxt(pChannel:int,pTxt:String):void
		{
			//锁屏
			if(stopAutoShowNum)
			{
				cacheTxt(pChannel,pTxt);
			}
			else
			{
				showTxt(pChannel,pTxt);
			}
		}
		
		/**
		 * 直接显示聊天内容 
		 * 
		 */
		private function showTxt(pChannel:int,pTxt:String):void
		{
			//直接显示在聊天中
			for(var j:int=0;j<_curShow.length;j++)
			{
				if(pChannel!=_curShow[j])
				{
					continue;
				}else
				{
				}
			}
		}
		
		/**
		 * 缓存聊天内容 
		 * 
		 */
		private function cacheTxt(pChannel:int,pTxt:String):void
		{
			if(_cacheArr.length > MAX_LINE)
			{
				_cacheArr.shift();
			}
			_cacheArr.push([pChannel,pTxt]);
		}
		
		/**
		 * 只在聊天窗口显示 
		 * @param evt
		 * 
		 */		
		private function onChatShow(evt:ChatEvent):void
		{
			var tpStr:String	= "";
			tpStr = StaticIni.getSwfLangStrVar("1000000260",[evt.clientMsg]);
			//保存内容显示数据
			var _tp:Array = [];
			_tp.push(CHANNEL_TIP);
			_tp.push(tpStr);
			_chatData.push(_tp);
			
			appendTxt(CHANNEL_TIP,tpStr);
			/*if(!stopAutoShowNum)
			{
				//解除【禁止滚屏】后聊天内容更新
				//chatContentModify();
				//直接显示在聊天中
				appendTxt(CHANNEL_TIP,tpStr);
			}*/
		}
		
		/**
		 * 在聊天窗口和中间文字效果都显示 
		 * @param evt
		 * 
		 */		
		private function onAllInfoShow(evt:ChatEvent):void
		{
			var tpStr:String	= "";
			tpStr = StaticIni.getSwfLangStrVar("1000000260",[evt.clientMsg]);
			//保存内容显示数据
			var _tp:Array = [];
			_tp.push(CHANNEL_TIP);
			_tp.push(tpStr);
			_chatData.push(_tp);
			
			appendTxt(CHANNEL_TIP,tpStr);
			/*if(!stopAutoShowNum)
			{
				//解除【禁止滚屏】后聊天内容更新
				//chatContentModify();
				//直接显示在聊天中
				appendTxt(CHANNEL_TIP,tpStr);
			}*/
			
			//需要显示效果的提示消息(客户端本地或者受到后台的系统消息提示)
			if(evt.effectMsg == "")
			{
				model.dispatchEvent(new ChatEvent(ChatEvent.ALERT_MESSAGE,evt.clientMsg));
			}else
			{
				model.dispatchEvent(new ChatEvent(ChatEvent.ALERT_MESSAGE,evt.effectMsg));
			}
		}
		
		/**
		 * 聊天链接点击 
		 * @param val
		 */
		private function onTextClick(e:TextEvent):void
		{
			var val:String = e.text;
			if((StringUtil.contains(val,"x=")>0) &&(StringUtil.contains(val,"y=")>0))
			{
				var seperate	:Array	=	val.split(",");
				var x	:Number	=	Number(StringUtil.afterLast(seperate[0],"="));
				seperate[1]	=	StringUtil.remove(seperate[1],")");
				var y	:Number	=	Number(StringUtil.afterLast(seperate[1],"="));
				
				otherEquipId.x	=	x;
				otherEquipId.y	=	y;
				//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LOOK_EQUIP,otherEquipId);
				_isShowTips	=	true;
				return;
			}
		}
		
		/**
		 * 聊天内容框信息更新 
		 * 
		 */		
		private function chatContentModify():void
		{
			var num	:int	=	_cacheArr.length;
			for(var i:int=0;i<num;i++)
			{
				var arr:Array	=	_cacheArr.shift();
				showTxt(arr.shift(),arr.shift());
			}
			_cacheArr	=	[];
		}
		
		/**
		 * 滚屏事件操作(鼠标点击)
		 * @param e
		 * 
		 */		
		private function stopAutoShow(e:MouseEvent):void
		{
			if(stopAutoShowNum==false)
			{
				stopAutoShowNum=true;
				_cacheArr	=	[];
				
			}else
			{
				stopAutoShowNum=false;
				//解除【禁止滚屏】后聊天内容更新
				chatContentModify();
			}
		}
		
		/**
		 * 好友系统私聊请求处理 
		 * @param e
		 * 
		 */		
		private function friendSysAskPrivateChat(evt:ChatEvent):void
		{
			var name:String		=	evt.clientMsg;
			secretChat = name;
		}
		
		private function tipShowNotPin(e:Event):void
		{
			//			chatText.htmlStr	+=	StaticIni.getSwfLangStrVar(getChannelStyle(CHANNEL_TIP),[]);
			
			//保存内容显示数据
			var _tp		:Array	=	[];	
			_tp.push(CHANNEL_TIP);
			_tp.push(StaticIni.getSwfLangStrVar(getChannelStyle(CHANNEL_TIP),[ ]));
			_chatData.push(_tp);
			
		}
		
		/**
		 * 本地屏蔽消息提示
		 * @param e
		 * 
		 */		
		private function tipShowPin(e:ChatEvent):void
		{
			var tpStr:String="";
			switch((e.currentTarget as ChatListCheckBoxItem).labName)
			{
				case	StaticIni.getSwfLangStr("1000000255"):	//"【私聊】已屏蔽";
					tpStr	=	StaticIni.getSwfLangStr(getChannelStyle(CHANNEL_TIP,1));
					break;
				case 	StaticIni.getSwfLangStr("1000000254"):	//"【帮会】已屏蔽";
					tpStr	=	StaticIni.getSwfLangStr(getChannelStyle(CHANNEL_TIP,4));
					break;	
			}
			
			for(var i:int=0;i<_curShow.length;i++)
			{
				if(_curShow[i]==(e as ChatEvent).cbId)
				{
					_curShow.splice(i,1);
				}
			}
			
			//保存内容显示数据
			var _tp		:Array	=	[];	
			_tp.push(CHANNEL_TIP);
			_tp.push(tpStr);
			_chatData.push(_tp);
			
			appendTxt(CHANNEL_TIP,tpStr);
			/*if(!stopAutoShowNum)
			{
				//解除【禁止滚屏】后聊天内容更新
				//chatContentModify();
				appendTxt(CHANNEL_TIP,tpStr);
			}*/
		}
		
		/**
		 * 本地取消屏蔽消息提示 
		 * @param e
		 * 
		 */		
		private function tipShowBackNotPin(evt:ChatEvent):void
		{
			var tmStr:String;
			switch((evt.currentTarget as ChatListCheckBoxItem).labName)
			{
				case	StaticIni.getSwfLangStr("1000000255"):	//"【私聊】取消屏蔽";
					tmStr	=	StaticIni.getSwfLangStr(getChannelStyle(CHANNEL_TIP,11));
					break;
				case 	StaticIni.getSwfLangStr("1000000254"):	//"【帮会】取消屏蔽";
					tmStr	=	StaticIni.getSwfLangStr(getChannelStyle(CHANNEL_TIP,44));
					break;	
			}
			
			//添加显示的频道
			_curShow.push((evt as ChatEvent).cbId);
			
			//保存内容显示数据
			var _tp		:Array	=	[];	
			_tp.push(CHANNEL_TIP);
			_tp.push(tmStr);
			_chatData.push(_tp);
			
			appendTxt(CHANNEL_TIP,tmStr);
			/*if(!stopAutoShowNum)
			{
				//解除【禁止滚屏】后聊天内容更新
				//chatContentModify();
				appendTxt(CHANNEL_TIP,tmStr);
			}*/
		}
		/**
		 * 
		 * @param name 聊天人的名字
		 * 
		 */			
		public function set secretChat(name:String):void
		{
			btn_current.label=StaticIni.getSwfLangStrVar("1000000255",[ ]);//私聊
			chatInput.htmlStr="/"+name+" ";
			chatInput.onFocus(chatInput.htmlStr.length,chatInput.htmlStr.length);
			channels=CHANNEL_PRIVATE;
		}
		
		/**
		 * 点击频道列表项事件，本地频道按钮显示更新操作 
		 * @param e
		 * 
		 */		
		private function onList(evt:ChatEvent):void
		{
			if(channels==CHANNEL_PRIVATE)
			{
				chatInput.htmlStr="";
			}
			switch(StaticIni.getSwfLangStr("1000000253"))
			{
//				case CHANNEL_CURRENT:
				case StaticIni.getSwfLangStr("1000000253"):
					chatInput.htmlStr="";
					btn_current.label=StaticIni.getSwfLangStr("1000000253");//当前
					channels=CHANNEL_CURRENT;
					break;
//				case CHANNEL_PRIVATE:
				case StaticIni.getSwfLangStr("1000000255"):
					btn_current.label=StaticIni.getSwfLangStr("1000000255");//私聊
					chatInput.htmlStr="/";
					chatInput.onFocus(chatInput.htmlStr.length,chatInput.htmlStr.length);
					channels=CHANNEL_PRIVATE;
					break;
//				case CHANNEL_PARTY:
				case StaticIni.getSwfLangStr("1000000254"):
					btn_current.label=StaticIni.getSwfLangStr("1000000254");//帮派
					chatInput.htmlStr="";
					channels=CHANNEL_PARTY;
					break;
			}
			chanelList.visible	=	false;
		}
		
		/**
		 * 点击本地频道按钮 频道列表是否显示操作 
		 * @param e
		 * 
		 */		
		private function onListShow(e:MouseEvent):void
		{
			if(chanelList.visible)
			{
				chanelList.visible=false;
			}
			else
			{
				chanelList.visible=true;
			}
		}
		
		/**
		 * 聊天内容区域清空
		 * @param e
		 * 
		 */		
		private function onClear(e:MouseEvent):void
		{
			//清空
			chatIsEmpty = true;
			//内容数据清空
			_chatData		=	[];	
			_cacheArr		=	[];	
		}
		
		/**
		 * 聊天光标
		 * @param e
		 * 
		 */		
		private function onInputMouseUP(e:MouseEvent):void
		{
			if(_isEquip)
			{
				var _str		:String	=chatInput.htmlStr;
				var _begIndex	:int	=_str.indexOf(_equipName);
				var _endIndex	:int	=_begIndex+_equipName.length;
				var _caretIndex	:int	=chatInput.inputTxt.caretIndex;
				var _selectBeg	:int	=chatInput.inputTxt.selectionBeginIndex;
				var _selectEnd	:int	=chatInput.inputTxt.selectionEndIndex;
				if(_selectBeg==_selectEnd)
				{
					if(_caretIndex>_begIndex && _caretIndex<_endIndex)
					{
						chatInput.inputTxt.setSelection(_endIndex,_endIndex);
					}
				}
				else
				{
					if(_selectBeg>_begIndex && _selectBeg<_endIndex)
					{
						_selectBeg	=	_begIndex;
						chatInput.inputTxt.setSelection(_selectBeg,_selectEnd);
					}
					if(_selectEnd>_begIndex && _selectEnd<_endIndex)
					{
						_selectEnd	=	_endIndex;
						chatInput.inputTxt.setSelection(_selectBeg,_selectEnd);
					}
				}
			}
		}
		
		/**
		 * 回车事件监听 
		 * @param evt
		 * 
		 */
		private function onKeyDownEnter(evt:KeyboardEvent):void
		{
			if(!_self.isShow)
			{
				return;
			}
			if(evt.keyCode==120)
			{
				clearFoucs(null);
			}
			if(evt.keyCode==Keyboard.ENTER&&!DebugWindow.isShow)
			{
				if(DebugWindow.isShow)
				{
					clearFoucs(null);
				}
				if(chatInput.htmlStr=="")
				{
					stage.focus	=	chatInput.inputTxt;
					if(!chatInput.inputTxt.hasEventListener(FocusEvent.FOCUS_OUT))
					{
						chatInput.inputTxt.addEventListener(FocusEvent.FOCUS_OUT, onFocusChange);
					}
				}
				else if(stage.focus	 !=	chatInput.inputTxt)
				{
					return;
				}
				else
				{
					//非私聊频道,信息发送后输入框清空
					if(channels!=CHANNEL_PRIVATE)
					{
						chatInput.htmlStr="";
						clearFoucs(null);
					}
				}
			}
		}
		
		/**
		 * 清除焦点 
		 */
		public function clearFoucs(pTarget:DisplayObject=null):void
		{
			chatInput.inputTxt.removeEventListener(FocusEvent.FOCUS_OUT, onFocusChange);
			/*if(null!=pTarget&&pTarget is InteractiveObject)
			{
			stage.focus	=	pTarget as InteractiveObject;
			}*/
			if(stage)
				stage.focus	=	null;
		}
		
		/**
		 * 按键事件操作
		 * @param e
		 * 
		 */		
		private function onKeyEvent(e:KeyboardEvent):void
		{
			var tp:String;
			//点击"Enter"键
			if(e.keyCode==13)
			{
				onEnter(null);	
				initFlag=0;
			}
			
			if(initFlag==0)
			{
				curSendMsgIndex	=	(sendMsg as Array).length;
				tpNotSendMsg	=	"";
				if(chatInput.htmlStr!="")
				{
					tpNotSendMsg	=	chatInput.htmlStr;
				}
			}
			//点击"上"键
			if(e.keyCode==38)
			{
				curSendMsgIndex--;
				initFlag++;
				if(curSendMsgIndex>-1)
				{
					chatInput.htmlStr	=	sendMsg[curSendMsgIndex];
				}else
				{
					curSendMsgIndex=0;
				}
				
			}
			//点击"下"键
			if(e.keyCode==40)
			{
				if(curSendMsgIndex<sendMsg.length)
				{
					curSendMsgIndex++;
				}
				
				if(curSendMsgIndex==sendMsg.length)
				{
					chatInput.htmlStr	=	tpNotSendMsg;
				}else
				{
					chatInput.htmlStr	=	sendMsg[curSendMsgIndex];
				}
			}
			//删除BackSpace：8||del：46
			if(_isEquip)
			{
				var _str		:String	=chatInput.htmlStr;
				var _begIndex	:int	=_str.indexOf(_equipName);
				var _endIndex	:int	=_begIndex+_equipName.length;
				var _caretIndex	:int	=chatInput.inputTxt.caretIndex;
				if(_caretIndex>=_begIndex && _caretIndex<=_endIndex)
				{
					if((_caretIndex>_begIndex && e.keyCode==8)||(_caretIndex<_endIndex && e.keyCode==46))
					{
						_str	=	_str.replace(_equipName,"");
						chatInput.htmlStr	=	_str;
						_isEquip	=	false;
					}
					else if(_caretIndex>_begIndex)
					{
						chatInput.inputTxt.setSelection(_endIndex,_endIndex);
					}
				}
			}
			
		}
		
		/**
		 * 初始化聊天内容信息，默认为空
		 * 
		 */		
		private function initChatContent():void
		{                                                                                                                                                   
			//			chatText.htmlStr="";
		}
		
		/**
		 * 初始化喇叭发布信息，默认为空
		 * 
		 */		
		private function initSpeakerContent():void
		{
			//			speakerText.htmlStr="";
		}
		
		/**
		 *当鼠标移开聊天框 
		 * @param e
		 * 
		 */		
		private function onChatOver(e:MouseEvent):void
		{
			TipsManage.hideTips();
		}
		
		/**
		 * 发送聊天信息
		 * @param e
		 * 
		 */		
		private function onEnter(e:MouseEvent):void
		{
			var privateChat	:String	=	"";
			var tmpStr		:String	=	"";
			if(chatInput.htmlStr!="")
			{
				//保存发送的最新10条信息
				(sendMsg as Array).push(chatInput.htmlStr);
				if((sendMsg as Array).length>10)
				{
					(sendMsg as Array).splice(0,1);
				}
				
				if(channels==CHANNEL_PRIVATE)
				{
					//私聊输入格式:"/"(默认)玩家姓名+"空格键"+内容
					//对内容进行过滤，提取出聊天对象姓名、聊天内容
					var style:Boolean = false;
					var temp:Array	=	chatInput.htmlStr.split("/");
					privateChat	=	String(chatInput.htmlStr).substr(0,String(chatInput.htmlStr).search(" ")+1);
					if((chatInput.htmlStr as String).charAt(0) == "/")
					{
						var index:uint = (chatInput.htmlStr as String).indexOf(" ");//空格位置索引
						recieverName = (chatInput.htmlStr as String).substring(1,index);
						if(recieverName!="")
						{
							sendContent = (chatInput.htmlStr as String).substring(index+1,(chatInput.htmlStr as String).length);
							style = true;
						}
					}
					var _tp		:Array	=	[];	
					//输入格式正确
					if(style)
					{	
						if(sendContent!="")
						{
							//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHAT,channels,sendContent,recieverName);
						}
						else
						{
							_tp.push(CHANNEL_TIP);
							tmpStr	=	StaticIni.getSwfLangStrVar(getChannelStyle(CHANNEL_TIP,7),[ ]);
							_tp.push(tmpStr);
							_chatData.push(_tp);
							
							appendTxt(channels,tmpStr);
						}
					}
					else//输入格式不正确
					{
						//保存内容显示数据
						_tp.push(CHANNEL_TIP);
						tmpStr	=	StaticIni.getSwfLangStrVar(getChannelStyle(CHANNEL_TIP,7),[ ]);
						_tp.push(tmpStr);
						_chatData.push(_tp);
						
						appendTxt(channels,tmpStr);
					}	

					chatInput.htmlStr	=	privateChat;
					var len	:int	=	chatInput.htmlStr.length;
					chatInput.onFocus(len,len);
				}
				else if(channels!=CHANNEL_PRIVATE)
				{
					
					var	content	:String	=	chatInput.htmlStr;
					if(StringUtil.contains(content,"]")>0)
					{
						if(_isEquip)
						{
							//装备文字处理
							var linkTxt		:String	=	_equipName;
							linkTxt		=	StringUtil.replace(linkTxt,"[","[<u>");
							linkTxt		=	StringUtil.replace(linkTxt,"]","</u>]");
							linkTxt	=	TipsTools.link_color(linkTxt,_equipColor);
							linkTxt	=	"<a href='event:"+_equipObjId+"'>"+linkTxt+"</a>";
							content	=	StringUtil.replace(content,_equipName,linkTxt);
							content	=	StringUtil.replace(content,"<","&#60");
						}
					}
					
					//非私聊频道,信息发送后输入框清空
					if(channels!=CHANNEL_PRIVATE)
					{
						chatInput.htmlStr="";
						clearFoucs(null);
					}
				}
				_isEquip	=	false;
			}
		}
		/**
		 * index当前频道，more当前频道内容多种显示属性：默认-1，为一种
		 * 针对私聊有两种，1:发送格式，2：接收格式
		 * 本地提示消息有很多种
		 * @param index
		 * @param more
		 * @return 
		 * 
		 */		
		private function getChannelStyle(index:int,more:int=-1):String
		{
			switch(index)
			{
				case CHANNEL_CURRENT:
					return "1000000257";
				case CHANNEL_PRIVATE:
				{
					if(more==1)
					{
						return "1000000258";
					}
					if(more==2)
					{
						return "1000000261";
					}
				}
				case CHANNEL_PARTY:
					return "1000000259";
				case CHANNEL_SYSTEM:
				{
					switch(more)
					{
						case CHANNEL_CURRENT:
						case CHANNEL_PRIVATE:
						case CHANNEL_PARTY:
							return "1000000548";
						case FLAG_PRIVATE_FAIL:
							return "";
					}
					return "1000000260";
				}
				case CHANNEL_TIP:
				{
					switch(more)
					{
						case 1:
							return "4002001001";//私聊屏蔽
						case 4:
							return "4002001005";//帮会屏蔽
							
						case 7:
							return "4002001021";//提示:私聊格式不正确
							
							
						case 11:
							return "4002001007";//私聊屏蔽取消
						case 44:
							return "4002001011";//帮会屏蔽取消
					}
					return "4002000999";//频道不可屏蔽
				}
			}
			return "";
		}
		
		private function replaceXML(val:String):String
		{
			if(_configXML==null)
			{
				return val;
			}
			var _len	:int = _configXML.icon.length();
			var _index	:int = -1;
			var _iconStr	:String;
			var _imgHTML	:String;
			if (_len <= 0) return null;
			
			for (var i:int = 0; i < _len; i++ ) {
				
				_iconStr = _configXML.icon[i].@iconStr;
				_index = val.indexOf(_iconStr);
				
				if (_index!=-1)
				{
					_imgHTML = "<img width='" + _configXML.icon[i].@width + "' height='" + _configXML.icon[i].@height + "' src='" + _configXML.icon[i].@iconUrl + "'>";
					val = StringUtil.replace(val,_iconStr,_imgHTML);
				}
			}
			return val;
		}
		
		/**
		 * 重写显示 
		 * @param space
		 * @param onshow
		 * @param argc
		 * 
		 */
		override public function show(space:String=BACKLAYER, onshow:Function=null):void
		{
			super.show(space,onshow);
		}
		
		/**
		 * 场景变化改变位置 
		 * 
		 */
		override protected function stageReSize(evt:Event=null):void
		{
			this.x	=	initX - GameStage.mainLayer.x;
			this.y = 	initY + GameStage.mainLayer.y;
		}
	}
}